package com.wyyx.api;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wyyx.model.Coupon;
import com.wyyx.services.CouponAddServices;

import commons.Constants;
import commons.JsonResult;
import commons.JsonResultWriter;

/**
 * 添加优惠券
 */
@WebServlet("/CouponAddServlet")
public class CouponAddServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Coupon coupon = new Coupon();
		coupon.setCouponName(request.getParameter("couponName"));
		coupon.setCouponDiscountPrice(request.getParameter("couponDiscountPrice"));
		coupon.setCouponLimitPrice(request.getParameter("couponLimitPrice"));
		coupon.setCouponStartTime(request.getParameter("startTime"));
		coupon.setCouponeEndTime(request.getParameter("endTime"));
		
		CouponAddServices couponAddServices = new CouponAddServices();
		int i = couponAddServices.addCoupon(coupon);
		JsonResult result = null;
		
		try {
			if(i>0) {
				result = new JsonResult<>("加入成功",Constants.STATUS_SUCCESS);
			}else {
				result = new JsonResult<>("加入失败",Constants.STATUS_NOTFOUND);
			}
		}catch(Exception e) {
				result = new JsonResult<>("加入异常",Constants.STATUS_EX);
		}
		
		JsonResultWriter.writer(response,result);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
